<template>
  <!-- 门店服务 -->
  <div class="outlet-service">
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('shop:provision:save')"
          class="default-btn primary-btn"
          @click="addOrUpdateHandle()">
          {{$t('crud.addTitle')}}
        </div>
        <!-- <div
          v-if="isAuth('shop:provision:export')"
          class="default-btn primary-btn"
          @click="exportService()">
          {{$t('crud.excelBtn')}}
        </div> -->
      </div>
      <div class="table-con hot-search-table">
        <el-table
          ref="outletServiceListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%">
          <el-table-column
            align="left"
            prop="name"
            :label="$t('outlet.serviceName')"
            min-width="150">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="left"
            prop="categoryName"
            :label="$t('outlet.classification')"
             min-width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.categoryName" class="table-cell-text">{{ scope.row.categoryName }}</span>
              <span v-else class="table-cell-text">{{ $t('outlet.allClassify') }}</span>
            </template>
          </el-table-column> -->
          <!-- 状态 1.正常 0.下线 -1.删除 -->
          <!-- <el-table-column
            align="center"
            prop="recDate"
            :label="$t('outlet.status')"
            min-width="200"
            sortable>
            <template slot-scope="scope">
              <span v-if="scope.row.status===-1" class="table-cell-text">
                {{ $t('outlet.delete') }}
              </span>
              <span v-if="scope.row.status===0" class="table-cell-text">
                {{ $t('outlet.offline') }}
              </span>
              <span v-if="scope.row.status===1" class="table-cell-text">
                {{ $t('outlet.normal') }}
              </span>
            </template>
          </el-table-column> -->


          <el-table-column
            align="center"
            prop="createTime"
            :label="$t('weixin.createtime')"
            min-width="200"
            sortable>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="240">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('shop:provision:update')"
                  class="default-btn text-btn"
                  @click="addOrUpdateHandle(scope.row.id)"
                >{{$t('user.editBtn')}}</div>
                <div
                  v-if="isAuth('shop:provision:delete')"
                  class="default-btn text-btn"
                  @click.stop="deleteHandle(scope.row.id)"
                >{{$t('user.deleteBtn')}}</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <addOrUpdate
      v-if="addOrUpdateVisible"
      @refreshChange="refreshChange"
      ref="addOrUpdate">
    </addOrUpdate>
  </div>

</template>

<script>
import addOrUpdate from './outlet-service-add-or-update.vue'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      addOrUpdateVisible: false
    }
  },
  components: {
    addOrUpdate
  },
  created () {
  },
  mounted () {
    this.getDataList()
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/shop/provision/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            }
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
        // 末尾页数据为空重新请求
        if (!this.dataList.length && this.page.currentPage > 1) {
          this.page.currentPage = 1
          this.getDataList()
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 导出服务
    exportService () {
      this.exportDisabled = true
      const loading = this.$loading({
        lock: true,
        target: '.table-con',
        customClass: 'export-load',
        background: 'transparent',
        text: this.$i18n.t('shop.exportIng')
      })
      this.$http({
        url: this.$http.adornUrl('/shop/provision/exportProvision'),
        method: 'get',
        params: this.$http.adornParams(this.searchForm),
        responseType: 'blob' // 解决文件下载乱码问题
      }).then(({ data }) => {
        loading.close()
        this.exportDisabled = false
        var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
        const fileName = this.$i18n.t('outlet.outletService')
        const elink = document.createElement('a')
        if ('download' in elink) { // 非IE下载
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      }).catch((e) => {
        loading.close()
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('outlet.doyouWantToDeleteTheService'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/shop/provision/' + id),
          method: 'delete',
          params: this.$http.adornParams()
        }).then(() => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success'
          })
          this.getDataList()
        })
      })
    },
    // 刷新回调用
    refreshChange () {
      this.getDataList(this.page)
    },
    /**
     * 重置表单
     * @param {String} formName 表单名称
     */
    resetSearchForm (formName) {
      this.$refs[formName].resetFields()
      this.prodName = ''
      this.isTop = ''
      this.categoryId = null
    },

    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      console.log(val)
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
